<template>
	<xn-form-container
		:title="formData.id ? '编辑期货数据表' : '增加期货数据表'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="交易日期：" name="tradeDate">
						<a-date-picker
							v-model:value="formData.tradeDate"
							value-format="YYYY-MM-DD"
							show-time
							placeholder="请选择交易日期"
							style="width: 100%"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="合约代码：" name="contractCode">
						<a-input v-model:value="formData.contractCode" placeholder="请输入合约代码" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="合约名称：" name="contractName">
						<a-input v-model:value="formData.contractName" placeholder="请输入合约名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="开盘价：" name="openPrice">
						<a-input v-model:value="formData.openPrice" placeholder="请输入开盘价" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="收盘价：" name="closePrice">
						<a-input v-model:value="formData.closePrice" placeholder="请输入收盘价" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="昨结：" name="yesterdayConclusion">
						<a-input v-model:value="formData.yesterdayConclusion" placeholder="请输入昨结" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="今结：" name="todayConclusion">
						<a-input v-model:value="formData.todayConclusion" placeholder="请输入今结" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="涨跌1：" name="upDownOne">
						<a-input v-model:value="formData.upDownOne" placeholder="请输入涨跌1" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="成交量：" name="finishNumber">
						<a-input v-model:value="formData.finishNumber" placeholder="请输入成交量" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="成交金额：" name="finishAmount">
						<a-input v-model:value="formData.finishAmount" placeholder="请输入成交金额" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="持仓量：" name="positionNumber">
						<a-input v-model:value="formData.positionNumber" placeholder="请输入持仓量" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="增减量：" name="changeNumber">
						<a-input v-model:value="formData.changeNumber" placeholder="请输入增减量" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="最高价：" name="highestPrice">
						<a-input v-model:value="formData.highestPrice" placeholder="请输入最高价" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="最低价：" name="lowestPrice">
						<a-input v-model:value="formData.lowestPrice" placeholder="请输入最低价" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="设置止损时间：" name="lossDate">
						<a-date-picker
							v-model:value="formData.lossDate"
							value-format="YYYY-MM-DD HH:mm:ss"
							show-time
							placeholder="请选择设置止损时间"
							style="width: 100%"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="是否开盘单：" name="openOrder">
						<a-select v-model:value="formData.openOrder" placeholder="请选择是否开盘单" :options="openOrderOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="是否收盘单：" name="closeOrder">
						<a-select v-model:value="formData.closeOrder" placeholder="请选择是否收盘单" :options="closeOrderOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="最后收益：" name="finalIncome">
						<a-input v-model:value="formData.finalIncome" placeholder="请输入最后收益" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="止损价：" name="lossPrice">
						<a-input v-model:value="formData.lossPrice" placeholder="请输入止损价" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="涨或跌：" name="upDown">
						<a-select v-model:value="formData.upDown" placeholder="请选择涨或跌" :options="upDownOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="是否止损单：" name="lossOrder">
						<a-select v-model:value="formData.lossOrder" placeholder="请选择是否止损单" :options="lossOrderOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="数据来源：" name="datesource">
						<a-input v-model:value="formData.datesource" placeholder="请输入数据来源" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="批次号：" name="batchId">
						<a-input v-model:value="formData.batchId" placeholder="请输入批次号" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="qhHistoryForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import qhHistoryApi from '@/api/biz/qhHistoryApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const openOrderOptions = ref([])
	const closeOrderOptions = ref([])
	const upDownOptions = ref([])
	const lossOrderOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		openOrderOptions.value = tool.dictList('Y_N')
		closeOrderOptions.value = tool.dictList('Y_N')
		upDownOptions.value = tool.dictList('Y_N')
		lossOrderOptions.value = tool.dictList('Y_N')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				qhHistoryApi
					.qhHistorySubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
@/api/biz/qhHistoryApi